﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DR5.SPExtend
{
    public static class StringEx
    {

        public const string SpecialChar = "\"/\\[]:|<>+=;,?*'@";
        public const string Key = "Key";
        public const string Value = "Value";
        public const string DateFormat_DMYHMS = "d-MMM-yyyy HH:mm:ss";
        public const string Title = "Title";
        public const string ID = "ID";

        /// <summary>
        /// Check the mutiple word or string in string with casing
        /// </summary>
        /// <param name="str"></param>
        /// <param name="strArray"></param>
        /// <returns></returns>
        public static bool ContainsWithCase(this string str, string[] strArray)
        {
            foreach (string s in strArray)
            {
                if (str.Contains(s))
                    return true;
            }
            return false;
        }

        public static bool Contains(this string str, string[] strArray)
        {
            str = str.ToLower();
            foreach (string s in strArray)
            {
                if (str.Contains(s.ToLower()))
                    return true;
            }
            return false;
        }

        public static string RemoveSpecialCharacters(this string str)
        {
            //One Way
            StringBuilder sb = new StringBuilder();
            foreach (char c in str)
            {
                if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') | c == '.' || c == '_' || c == ' ')
                {
                    sb.Append(c);
                }
                else
                {
                    sb.Append(' ');
                }
            }
            return sb.ToString();
            //One Way Completed
        }

        public static string ToUpperCaseFirst(this string s)
        {
            // Check for empty string.
            if (string.IsNullOrEmpty(s))
            {
                return string.Empty;
            }
            // Return char and concat substring.
            return char.ToUpper(s[0]) + s.Substring(1);
        }

        public static string RemoveHtml(this string text)
        {
            return System.Text.RegularExpressions.Regex.Replace(text, @"<(.|\n)*?>", string.Empty);
        }

        public static bool IsNumeric(this String str)
        {
            try
            {
                Double.Parse(str.ToString());
                return true;
            }
            catch
            {
            }
            return false;
        }

        public static int ToInt32(this string str)
        {
            try
            {
                return Convert.ToInt32(str);
            }
            catch
            {
                return 0;
            }
        }

        public static int[] ToInt32(this string[] array)
        {
            int[] intarray = new Int32[array.Length];
            for (int i = 0; i < array.Length; i++)
            {
                intarray[i] = array[i].ToInt32();
            }
            return intarray;
        }

        public static string[] Split(this string str, string splitBy)
        {
            return str.Split(new string[] { splitBy }, StringSplitOptions.None);
        }

        public static bool IsNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }

        public static bool IsNotNullOrEmpty(this string str)
        {
            return !string.IsNullOrEmpty(str);
        }
    }
}
